<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 网络事件处理器深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            margin: 1.5rem 0;
            border-radius: 0.5rem;
        }
        
        code {
            background: #f3f4f6;
            padding: 0.2rem 0.4rem;
            border-radius: 0.25rem;
            font-family: 'Courier New', monospace;
            color: #764ba2;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Redis 网络事件处理器
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入解析高性能背后的核心机制
                </p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-bolt mr-2"></i>
                        <span>事件驱动</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-network-wired mr-2"></i>
                        <span>I/O 多路复用</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-tachometer-alt mr-2"></i>
                        <span>高并发处理</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">R</span>edis 的网络事件处理器是 Redis 中处理网络请求的核心组件之一。它负责处理客户端的请求、事件通知以及管理网络连接。了解 Redis 的网络事件处理器有助于理解 Redis 的高性能和高并发处理能力。
                </p>
            </div>

            <!-- Event-Driven Model -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-sync-alt feature-icon mr-3"></i>事件驱动模型
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    Redis 使用事件驱动模型来处理网络事件，这意味着它使用异步非阻塞的方式来处理所有客户端请求。事件驱动模型的关键组件包括：
                </p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="highlight-box">
                        <h3 class="font-bold text-xl mb-2 text-gray-800">
                            <i class="fas fa-circle-notch text-purple-600 mr-2"></i>事件循环（Event Loop）
                        </h3>
                        <p class="text-gray-700">Redis 的事件循环是处理事件的核心部分，它持续运行并检查是否有新的网络事件需要处理。</p>
                    </div>
                    <div class="highlight-box">
                        <h3 class="font-bold text-xl mb-2 text-gray-800">
                            <i class="fas fa-random text-purple-600 mr-2"></i>事件分发器（Event Dispatcher）
                        </h3>
                        <p class="text-gray-700">负责将不同类型的事件（如读事件、写事件、超时事件等）分发给相应的处理器。</p>
                    </div>
                </div>
            </div>

            <!-- I/O Multiplexing -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-project-diagram feature-icon mr-3"></i>I/O 多路复用
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    Redis 使用 I/O 多路复用技术来高效地处理多个客户端的网络请求。I/O 多路复用允许单个线程监视多个网络连接的读写状态，从而减少了线程上下文切换的开销。
                </p>
                
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-lg">
                        <div class="text-center mb-3">
                            <i class="fab fa-linux text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2 text-center">epoll</h3>
                        <p class="text-sm text-gray-700 text-center">Linux 系统默认使用，高效的 I/O 多路复用机制</p>
                    </div>
                    <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-lg">
                        <div class="text-center mb-3">
                            <i class="fab fa-apple text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2 text-center">kqueue</h3>
                        <p class="text-sm text-gray-700 text-center">macOS 和 BSD 系统使用的高性能机制</p>
                    </div>
                    <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-lg">
                        <div class="text-center mb-3">
                            <i class="fas fa-server text-4xl text-purple-600"></i>
                        </div>
                        <h3 class="font-bold text-lg mb-2 text-center">select/poll</h3>
                        <p class="text-sm text-gray-700 text-center">兼容性方案，适用于较老的系统环境</p>
                    </div>
                </div>
            </div>

            <!-- Event Processing Flow -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-stream feature-icon mr-3"></i>事件处理流程
                </h2>
                
                <div class="mermaid">
                    graph TD
                        A[客户端连接] --> B[注册事件]
                        B --> C{事件循环}
                        C --> D[等待事件]
                        D --> E{事件触发?}
                        E -->|是| F[处理事件]
                        E -->|否| D
                        F --> G[解析请求]
                        G --> H[执行命令]
                        H --> I[发送响应]
                        I --> C
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style I fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                </div>
                
                <div class="mt-8 space-y-4">
                    <div class="flex items-start">
                        <span class="flex-shrink-0 w-8 h-8 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">1</span>
                        <div>
                            <h4 class="font-bold text-lg mb-1">注册事件</h4>
                            <p class="text-gray-700">当有客户端连接到 Redis 服务器时，Redis 会为该连接注册读事件和写事件。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <span class="flex-shrink-0 w-8 h-8 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">2</span>
                        <div>
                            <h4 class="font-bold text-lg mb-1">事件循环</h4>
                            <p class="text-gray-700">事件循环使用 I/O 多路复用技术等待网络事件的发生，一旦事件被触发，调用相应的事件处理函数。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <span class="flex-shrink-0 w-8 h-8 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">3</span>
                        <div>
                            <h4 class="font-bold text-lg mb-1">请求解析和处理</h4>
                            <p class="text-gray-700">事件处理函数将客户端发送的请求解析为 Redis 命令，执行相应的操作并生成响应。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <span class="flex-shrink-0 w-8 h-8 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">4</span>
                        <div>
                            <h4 class="font-bold text-lg mb-1">发送响应</h4>
                            <p class="text-gray-700">事件处理函数将命令的执行结果发送回客户端。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Multi-threading Support -->
            <div class="content-card rounded-xl shadow-xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-layer-group feature-icon mr-3"></i>多线程支持
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    虽然 Redis 本身是单线程的，但它可以通过某些机制来有效地利用系统资源：
                </p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-gradient-to-r from-purple-100 to-indigo-100 p-6 rounded-lg">
                        <h3 class="font-bold text-xl mb-3">
                            <i class="fas fa-microchip text-purple-600 mr-2"></i>I/O 线程
                        </h3>
                        <p class="text-gray-700">从